package com.zjp.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjp.pojo.Healthreport;
import com.zjp.service.HealthreportService;
import com.zjp.utils.QRCodeServiceUtils;
import com.zjp.pojo.User;
import com.zjp.vo.HealthDeclarationRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 健康管理
 */
@RestController
@Slf4j
public class QRCodeController {
    @Autowired
    private QRCodeServiceUtils qrCodeService;
    @Autowired
    private HealthreportService healthreportService;

    /**
     * 生成健康二维码
     */
    @GetMapping("qrCode")
    public void getQRCode(Integer healthStatus, HttpServletResponse response, HttpSession session) {
        User user = (User) session.getAttribute("user");
        String codeContent = "姓名:" + user.getName() + ",身份证:" + user.getIdcard();
        try {
            qrCodeService.createQRCode2Stream(healthStatus, codeContent, response, user.getPic());
            log.info("生成健康二维码成功{}", codeContent);
        } catch (Exception e) {
            log.error("生成健康二维码失败");
        }
    }

    /**
     * 健康上报查询
     */
    @RequestMapping("findReportProvince")
    public ModelAndView findReportProvince(HttpSession session) {
        User user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        List<Healthreport> healthreports = healthreportService.findReportProvince(user.getId());
        log.info("查询健康上报查询{}", healthreports);
        modelAndView.addObject("healthreports", healthreports);
        modelAndView.addObject("user", user);
        modelAndView.setViewName("healthreports");
        return modelAndView;
    }


    /**
     * 健康上报提交
     */
    @RequestMapping("/addHealthreport")
    public String addHealthreport(Healthreport healthreport) {
        if(StringUtils.isEmpty(healthreport.getDeparturearea())){
            return "ERROR";
        }
        if (StringUtils.isEmpty(healthreport.getArrivalarea())) {
            return "ERROR";
        }
        if (StringUtils.isEmpty(healthreport.getAddress())){
            return "ERROR";
        }
        healthreport.setReportingtime(new Date());
        int i = healthreportService.addHealthreport(healthreport);
        if (i > 0) {
            log.info("健康上报成功{}", healthreport);
            return "SUCCESS";

        }
        return "ERROR";
    }


    /**
     * 健康上报记录查询
     */
    @RequestMapping("HealthDeclarationRecord")
    public ModelAndView HealthDeclarationRecord(HttpSession session, HealthDeclarationRecordVo healthDeclarationRecordVo,
                                                @RequestParam(name = "pageNo", required = false, defaultValue = "1") int pageNo,
                                                @RequestParam(name = "pageSize", required = false, defaultValue = "5") int pageSize) {
        User user = (User) session.getAttribute("user");
        System.out.println("用户id为："+user.getId());
        ModelAndView modelAndView = new ModelAndView();
         PageHelper.startPage(pageNo, pageSize);//开启分页
        List<HealthDeclarationRecordVo> healthDeclarationRecordVos = healthreportService.HealthDeclarationRecord(user.getId(),healthDeclarationRecordVo);
        log.info("查询健康上报记录查询{}", healthDeclarationRecordVos);
        modelAndView.addObject("healthDeclarationRecordVos", healthDeclarationRecordVos);
        PageInfo pageInfo = new PageInfo<>(healthDeclarationRecordVos);  //存储分页信息
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("healthDeclarationRecord");
        return modelAndView;
    }
}
